*** Barberia et al. (2022)
*** Brazilian Political Science Review
*** "The Relationship between Ideology and COVID-19 Deaths: What We Know and What We Still Need to Know"
*** Figures 10 & 11

clear
cd "/Users/nataliamoreira/Dropbox/Replication BPSR"

use "Data/voteshare_covidcasesdeaths.dta", clear

* Data management 

merge m:1 códigoibge using "Data/pibpercapita"
drop _merge

* Creating a data set for each round of 2018 Elections

* 1st round

preserve
keep if turno == 1
xtset códigoibge data, delta(7)
save "Data/mun_cases_deaths_bolsonaro1st", replace
restore

* 2nd round

preserve
keep if turno == 2
xtset códigoibge data, delta(7)
save "Data/mun_cases_deaths_bolsonaro2nd", replace
restore

* Creating a new data set in which we save the coefficient estimate from the 
* regression of votes on COVID-19 cases and deaths over time

* 1st round

capture postutil clear
tempname regbolso_1st
postfile `regbolso_1st' week b_casos_bolsonarovotos se_casos_bolsonarovotos /*
*/ b_mortes_bolsonarovotos se_mortes_bolsonarovotos using "Data/mun_bolsonaro1st", replace

foreach i in 202014 202018 202023 202027 202031 202036 202040 202043 202049 202053 /*
	*/ 202105 202109 202113 202117 202122 {
	
	use "Data/mun_cases_deaths_bolsonaro1st", clear
	xtset códigoibge data, delta(7)
	
	keep if epidemiological_week == `i'
	scalar week = `i'
	
	encode uf, g(state)
	
	reg casos_100k bolsonarovotos 
	scalar b_casos_bolsonarovotos = _b[bolsonarovotos]
	scalar se_casos_bolsonarovotos = _se[bolsonarovotos]
	
	reg mortes_100k bolsonarovotos 
	scalar b_mortes_bolsonarovotos = _b[bolsonarovotos]
	scalar se_mortes_bolsonarovotos = _se[bolsonarovotos]
				
	post `regbolso_1st' (week) (b_casos_bolsonarovotos) (se_casos_bolsonarovotos) /*
	*/ (b_mortes_bolsonarovotos) (se_mortes_bolsonarovotos) 
	} 
postclose `regbolso_1st'	

* 2nd round

capture postutil clear
tempname regbolso_2nd
postfile `regbolso_2nd' week b_casos_bolsonarovotos se_casos_bolsonarovotos /*
*/ b_mortes_bolsonarovotos se_mortes_bolsonarovotos using "Data/mun_bolsonaro2nd", replace

foreach i in 202014 202018 202023 202027 202031 202036 202040 202043 202049 202053 /*
	*/ 202105 202109 202113 202117 202122 {
	
	use "Data/mun_cases_deaths_bolsonaro2nd", clear
	xtset códigoibge data, delta(7)
	
	keep if epidemiological_week == `i'
	scalar week = `i'
	
	encode uf, g(state)
	
	reg casos_100k bolsonarovotos 
	scalar b_casos_bolsonarovotos = _b[bolsonarovotos]
	scalar se_casos_bolsonarovotos = _se[bolsonarovotos]
	
	reg mortes_100k bolsonarovotos 
	scalar b_mortes_bolsonarovotos = _b[bolsonarovotos]
	scalar se_mortes_bolsonarovotos = _se[bolsonarovotos]
				
	post `regbolso_2nd' (week) (b_casos_bolsonarovotos) (se_casos_bolsonarovotos) /*
	*/ (b_mortes_bolsonarovotos) (se_mortes_bolsonarovotos) 
	} 
postclose `regbolso_2nd'

* Figures

// First round

* Time series dataset:
use "Data/mun_bolsonaro1st.dta", clear

* 95% CI Cases
g l_casos_bolsonarovotos = b_casos_bolsonarovotos - (1.96*se_casos_bolsonarovotos)
g h_casos_bolsonarovotos = b_casos_bolsonarovotos + (1.96*se_casos_bolsonarovotos)

* 95% CI Deaths
g l_mortes_bolsonarovotos = b_mortes_bolsonarovotos - (1.96*se_mortes_bolsonarovotos)
g h_mortes_bolsonarovotos = b_mortes_bolsonarovotos + (1.96*se_mortes_bolsonarovotos)

g t = _n
label define datel 1 "2020-03-31" 2 "2020-04-30" 3 "2020-05-31" 4 "2020-06-30" 5 "2020-07-31" /*
*/ 6 "2020-08-31" 7 "2020-09-30" 8 "2020-10-24" 9 "2020-11-30" 10 "2020-12-31" /*
*/ 11 "2021-01-31" 12 "2021-02-28" 13 "2021-03-31" 14 "2021-04-30" 15 "2021-05-31"
label values t datel 

label variable t "month"

* Figure 10

* The effect of Bolsonaro vote share on deaths per 100,000

twoway rcap h_mortes_bolsonarovotos l_mortes_bolsonarovotos t if t<9, yline(0, lcolor(red) lpattern(dash)) msize(vsmall) /*
	*/ xlabel(1(1)8,labs(vsmall) valuelabel) /*
	*/ title("", size(medium)) xtitle("") /*
	*/ ylabel(,labs(small) nogrid) color(black) /*
	*/ legend(off) ytitle("Estimated effect") || scatter b_mortes_bolsonarovotos t if t<9, msize(medlarge) mcolor(black) msymbol(o)	

graph export "Figures/figure10.png", replace
	
* Figure 11

* The effect of Bolsonaro vote share on deaths per 100,000

twoway rcap h_mortes_bolsonarovotos l_mortes_bolsonarovotos t if t>8, yline(0, lcolor(red) lpattern(dash)) msize(vsmall) /*
	*/ xlabel(9(1)15,labs(vsmall) valuelabel) /*
	*/ title("", size(medium)) xtitle("") /*
	*/ ylabel(,labs(small) nogrid) color(black) /*
	*/ legend(off) ytitle("Estimated effect") || scatter b_mortes_bolsonarovotos t if t>8, msize(medlarge) mcolor(black) msymbol(o)	
	
graph export "Figures/figure11.png", replace
	
